<script>
var opt_id = '{{.opt_id}}';
var box_type = '{{.box_type}}';

$('.tab-lei').find('[data-type="'+ box_type +'"]').addClass('active');
$('.chufang').find('[data-type="'+ box_type +'"]').show();
$.post('{{ urlfor "OptometryController.View" }}?opt_id='+opt_id, function (res) {
    if(res.code == 1) {
        layer.alert(res.msg,function () {
            history.back();
        });
        return false;
    }
    opt = res.data;
    $('#realname').val(opt.Archives.Realname);
    $('#mobile').val(opt.Archives.Mobile);
    $('input[value="'+ opt.Archives.Sex +'"]').prop('checked', true);
    $('#doctor').val(opt.Doctor);
    $('#optometrist').val(opt.Optometrist);
    $('#remarks').val(opt.Remarks);
    $('#opt_id').val(opt.Id);

    var chufang = opt.chufang;
    var chufang_info = $('#chufang_info');
    if(chufang['far']) {
        setBoxText(chufang_info.find('[data-type="far"]').find('[data-type="right"]'), chufang['far']['right']);  //右眼
        setBoxText(chufang_info.find('[data-type="far"]').find('[data-type="left"]'), chufang['far']['left']);  //左眼
        chufang_info.find('[data-type="far"]').find('[data-type="total_pd_yuan"]').find('b').text(chufang['far']['right'].total_pd_yuan);
        chufang_info.find("div.chufang").find(".datatable-shili[data-type='far']").attr("saved","true");
    }

    if(chufang['near']) {
        setBoxText(chufang_info.find('[data-type="near"]').find('[data-type="right"]'), chufang['near']['right']);  //右眼
        setBoxText(chufang_info.find('[data-type="near"]').find('[data-type="left"]'), chufang['near']['left']);  //左眼
        chufang_info.find('[data-type="near"]').find('[data-type="total_pd_jin"]').find('b').text(chufang['near']['left'].total_pd_jin);
        chufang_info.find("div.chufang").find(".datatable-shili[data-type='near']").attr("saved","true");
    }
    if(chufang['yinxing']) {
        setBoxText(chufang_info.find('[data-type="yinxing"]').find('[data-type="right"]'), chufang['yinxing']['right']);  //右眼
        setBoxText(chufang_info.find('[data-type="yinxing"]').find('[data-type="left"]'), chufang['yinxing']['left']);  //左眼
        chufang_info.find("div.chufang").find(".datatable-shili[data-type='yinxing']").attr("saved","true");
    }

    if(chufang['duojiaodian']) {
        setBoxText(chufang_info.find('[data-type="duojiaodian"]').find('[data-type="right"]'), chufang['duojiaodian']['right']);  //右眼
        setBoxText(chufang_info.find('[data-type="duojiaodian"]').find('[data-type="left"]'), chufang['duojiaodian']['left']);  //左眼
        chufang_info.find('[data-type="duojiaodian"]').find('[data-type="total_pd_jin"]').find('b').text(chufang['duojiaodian']['right'].total_pd_jin);
        chufang_info.find('[data-type="duojiaodian"]').find('[data-type="total_pd_yuan"]').find('b').text(chufang['duojiaodian']['right'].total_pd_yuan);
        chufang_info.find("div.chufang").find(".datatable-shili[data-type='duojiaodian']").attr("saved","true");
    }
    $('select').trigger('changed.selected.amui');
}, 'json');

function setBoxText(obj, data) {
    var _this = $(obj);
    $.each(data, function (index, value) {
        _this.find('[data-type="'+ index +'"]').text(value);
    });
}

//处方内容清除
$("#chufang_info").on("click", ".chufang > .datatable-shili > .clear-btn", function() {
    $(this).parent().attr("saved","fales");
    $(this).parent().find(".info-bar[data-type] > .item").text("");
    $(this).parent().find(".info-bar.define-row > .item > .pmr > b").text("");
});
//验光处方具体光度块点击
$("#chufang_info").on("blur", ".chufang > .datatable-shili > .info-bar > .item[data-type]", function() {
    var box = $("#chufang_info");
    var p_type=$(this).parent().parent().attr("data-type");
    var d_type = $(this).data('type');
    var new_va = $(this).text();
    if(new_va == '') {
        return;
    }
    if((p_type=="far"||p_type=="near"||p_type=="duojiaodian")&&(d_type=="pd_yuan"||d_type=="pd_jin")){
        box.find("div.chufang").find(".datatable-shili[data-type='"+p_type+"']").find("div[data-type='left']").next().find(".item > .pmr[data-type='total_"+d_type+"'] > b").text("");
        var total=0;
        $(this).parents(".datatable-shili").find("div.item[data-type='"+d_type+"']").each(function(){
            total = Number($(this).text())+total;
        });
        box.find("div.chufang").find(".datatable-shili[data-type='"+p_type+"']").find("div[data-type='left']").next().find(".item > .pmr[data-type='total_"+d_type+"'] > b").text(total);
    }
    if(d_type=="sph"||d_type=="cyl"){//球镜柱镜数值处理下
        new_va = formatnumber(new_va, "0.00");
        if(new_va>0){
            new_va = "+"+new_va;
        }else if(new_va==0){
            new_va  = new_va.toString()+".00";
        }
    }else if(d_type=="nv"||d_type=="cv"){
        new_va = formatnumber(new_va, "0.0");
    }
    $(this).text(new_va);
});

$('#submit_btn').click(function(){
	$('#formDoSubmitThis').submit();
	return false;
});
$('#formDoSubmitThis').submit(function(){
	var formValidity = $(this).validator('isFormValid');
	if(formValidity) { //通过检查
        save();
	}
	return false;	
});


function save() {
    var data={};
    /*****************组装验光数据**********************/
    data.optometry_and_optician_info={};
    var far =[];
    var naer=[];
    var yinxing=[];
    var dojiaodian=[];
    data.optometry_and_optician_info.far_info=far;
    data.optometry_and_optician_info.near_info =naer;
    data.optometry_and_optician_info.yinxing_info=yinxing;
    data.optometry_and_optician_info.duojiaodian_info=dojiaodian;
    var o_json ={};
    var box = $("#chufang_info");
    box.find("div.chufang").find(".datatable-shili[data-type='far'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'far';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_yuan= $(this).find("div[data-type='pd_yuan']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_yuan = box.find("div.chufang").find(".datatable-shili[data-type='far'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_yuan']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            far.push(o_json);
        }
    });

    box.find("div.chufang").find(".datatable-shili[data-type='near'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'near';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_jin = box.find("div.chufang").find(".datatable-shili[data-type='near'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_jin']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            naer.push(o_json);
        }
    });

    box.find("div.chufang").find(".datatable-shili[data-type='yinxing'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'yinxing';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.diameter = $(this).find("div[data-type='diameter']").text();
            o_json.base_curve = $(this).find("div[data-type='base_curve']").text();

            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            yinxing.push(o_json);
        }
    });
    box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar").each(function(){
        if($(this).attr("data-type")){
            o_json={};
            o_json.type_name = 'duojiaodian';
            o_json.sph = $(this).find("div[data-type='sph']").text();
            o_json.cyl = $(this).find("div[data-type='cyl']").text();
            o_json.axial_view = $(this).find("div[data-type='axial_view']").text();
            o_json.pd_jin= $(this).find("div[data-type='pd_jin']").text();
            o_json.pd_yuan= $(this).find("div[data-type='pd_yuan']").text();
            o_json.mrp = $(this).find("div[data-type='mrp']").text();
            o_json.nv = $(this).find("div[data-type='nv']").text();
            o_json.cv = $(this).find("div[data-type='cv']").text();
            o_json.add_info = $(this).find("div[data-type='add_info']").text();
            o_json.pass = $(this).find("div[data-type='pass']").text();
            o_json.v = $(this).find("div[data-type='v']").text();
            o_json.bc = $(this).find("div[data-type='bc']").text();
            o_json.total_pd_jin = box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_jin']").find("b").text();
            o_json.total_pd_yuan = box.find("div.chufang").find(".datatable-shili[data-type='duojiaodian'][saved='true']").find(".info-bar:last").find("[data-type='total_pd_yuan']").find("b").text();
            if($(this).attr("data-type")=="left"){
                o_json.eye_type = 'left';
            }else if($(this).attr("data-type")=="right"){
                o_json.eye_type = 'right';
            }
            dojiaodian.push(o_json);
        }
    });
    /****************************************/
    if (data.optometry_and_optician_info.duojiaodian_info.length <= 0
        && data.optometry_and_optician_info.yinxing_info.length <= 0
        && data.optometry_and_optician_info.near_info.length <= 0
        && data.optometry_and_optician_info.far_info.length <= 0) {
        layer.alert("请填写处方详情");
        return;
    }

    var doctor = $('#doctor').val();
    var optometrist = $('#optometrist').val();
    var remarks = $('#remarks').val();
    if(opt_id == 0) {
        layer.alert("参数错误");
        return;
    }
    var _data = {};
    _data.opt_id = opt_id;
    _data.doctor = doctor;
    _data.optometrist = optometrist;
    _data.remarks = remarks;

    _data.chufang = $.merge([], data.optometry_and_optician_info.duojiaodian_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.yinxing_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.near_info);
    _data.chufang = $.merge(_data.chufang, data.optometry_and_optician_info.far_info);
    _data.chufang = JSON.stringify(_data.chufang);

    var url = '{{urlfor "OptometryController.Edit"}}?opt_id='+opt_id+'&box_type='+box_type;
    $.post(url, _data, function (res) {
        if(res.code == 1) {
            layer.alert(res.msg);
            return false;
        }
        layer.alert('验光处方保存成功', function (index) {
            parent.location.reload();
            layer.close(index);
        });
    });
}
</script>